package com.ruoyi.busi.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.busi.domain.BusiCompanyFiling;
import com.ruoyi.busi.domain.bo.BusiCompanyFilingBo;
import com.ruoyi.busi.domain.vo.BusiCompanyFilingVo;
import com.ruoyi.busi.mapper.BusiCompanyFilingMapper;
import com.ruoyi.busi.service.IBusiCompanyFilingService;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import org.hyperledger.fabric.gateway.Contract;
import org.hyperledger.fabric.gateway.ContractException;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.UUID;
import java.util.concurrent.TimeoutException;

/**
 * 企业排放填报Service业务层处理
 *
 * @author ruoyi
 * @date 2024-11-24
 */
@Service
public class BusiCompanyFilingServiceImpl extends ServicePlusImpl<BusiCompanyFilingMapper, BusiCompanyFiling, BusiCompanyFilingVo> implements IBusiCompanyFilingService {

    @Resource
    private Contract contract;

    @Override
    public BusiCompanyFilingVo queryById(String id){
        return getVoById(id);
    }

    @Override
    public TableDataInfo<BusiCompanyFilingVo> queryPageList(BusiCompanyFilingBo bo) {
        PagePlus<BusiCompanyFiling, BusiCompanyFilingVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo));
        TableDataInfo<BusiCompanyFilingVo> s = PageUtils.buildDataInfo(result);
        for(BusiCompanyFilingVo company:s.getRows()){
            company.setFile(null);
        }
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<BusiCompanyFilingVo> queryList(BusiCompanyFilingBo bo) {
        return listVo(buildQueryWrapper(bo));
    }

    private LambdaQueryWrapper<BusiCompanyFiling> buildQueryWrapper(BusiCompanyFilingBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BusiCompanyFiling> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), BusiCompanyFiling::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getIndustry()), BusiCompanyFiling::getIndustry, bo.getIndustry());
        lqw.eq(StringUtils.isNotBlank(bo.getProvince()), BusiCompanyFiling::getProvince, bo.getProvince());
        lqw.eq(StringUtils.isNotBlank(bo.getAnnual()), BusiCompanyFiling::getAnnual, bo.getAnnual());
        lqw.eq(StringUtils.isNotBlank(bo.getPactId()), BusiCompanyFiling::getPactId, bo.getPactId());
        lqw.like(StringUtils.isNotBlank(bo.getPactName()), BusiCompanyFiling::getPactName, bo.getPactName());
        lqw.eq(StringUtils.isNotBlank(bo.getFile()), BusiCompanyFiling::getFile, bo.getFile());
        lqw.like(StringUtils.isNotBlank(bo.getCreateName()), BusiCompanyFiling::getCreateName, bo.getCreateName());
        lqw.like(StringUtils.isNotBlank(bo.getCreateChargeName()), BusiCompanyFiling::getCreateChargeName, bo.getCreateChargeName());
        lqw.eq(StringUtils.isNotBlank(bo.getSb1()), BusiCompanyFiling::getSb1, bo.getSb1());
        lqw.eq(StringUtils.isNotBlank(bo.getSb2()), BusiCompanyFiling::getSb2, bo.getSb2());
        lqw.eq(StringUtils.isNotBlank(bo.getSb3()), BusiCompanyFiling::getSb3, bo.getSb3());
        lqw.eq(StringUtils.isNotBlank(bo.getSb4()), BusiCompanyFiling::getSb4, bo.getSb4());
        lqw.eq(StringUtils.isNotBlank(bo.getSb5()), BusiCompanyFiling::getSb5, bo.getSb5());
        lqw.eq(StringUtils.isNotBlank(bo.getSb6()), BusiCompanyFiling::getSb6, bo.getSb6());
        return lqw;
    }

    @Override
    public Boolean insertByBo(BusiCompanyFilingBo bo) {
        BusiCompanyFiling add = BeanUtil.toBean(bo, BusiCompanyFiling.class);
        validEntityBeforeSave(add);
        boolean flag = save(add);

        byte[] queryAResultBefore = new byte[0];
//        String desc = "企业(" + add.getName() + ")填报" + add.getAnnual() + "排放合约,填报内容:" + add.getFile();
        String desc = "企业(" + add.getName() + ")填报" + add.getAnnual() + "排放合约,填报内容:" ;
        try {
            queryAResultBefore = contract.submitTransaction("CreateAsset",
                UUID.randomUUID().toString(),
                StrUtil.emptyIfNull(add.getPactId()),
                StrUtil.emptyIfNull(add.getPactName()),
                StrUtil.emptyIfNull(add.getAnnual()),
                "0",
                StrUtil.emptyIfNull(desc),
                StrUtil.emptyIfNull("0.00"));
            System.out.println("填报数据下发区块链:" + new String(queryAResultBefore, "utf-8"));
        } catch (ContractException | TimeoutException | InterruptedException | UnsupportedEncodingException e) {
            throw new ServiceException("向区块链提交数据失败,请检查区块链网络是否正常!");
        }

        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    @Override
    public Boolean updateByBo(BusiCompanyFilingBo bo) {
        BusiCompanyFiling update = BeanUtil.toBean(bo, BusiCompanyFiling.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(BusiCompanyFiling entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
